package com.hbsd.paopaobackend.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbsd.paopaobackend.common.BaseResponse;
import com.hbsd.paopaobackend.common.ErrorCode;
import com.hbsd.paopaobackend.common.ResultUtils;
import com.hbsd.paopaobackend.exception.BusinessException;
import com.hbsd.paopaobackend.model.domain.User;
import com.hbsd.paopaobackend.model.request.Blog.AddBlogRequest;
import com.hbsd.paopaobackend.model.request.Blog.BlogListRequest;
import com.hbsd.paopaobackend.model.request.Blog.SelectBlogRequest;
import com.hbsd.paopaobackend.model.vo.Blog.BlogListVO;
import com.hbsd.paopaobackend.model.vo.Blog.SelectBlogVO;
import com.hbsd.paopaobackend.service.BlogService;
import com.hbsd.paopaobackend.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Api(tags = "博客模块")
@RestController
@RequestMapping("/blog")
// 配置跨域 allowCredentials: 允许携带cookie
@CrossOrigin(origins = {"http://localhost:5173"}, allowCredentials = "true")

public class BlogController {
    
    @Resource
    private UserService userService;

    @Resource
    private BlogService blogService;


    /**
     * 获取博客列表
     * @param addBlogRequest
     * @param request
     * @return
     */
    @ApiOperation(value="添加博客", notes="添加博客 addBlog")
    @PostMapping("/add")
    public BaseResponse<Boolean> addBlog(@RequestBody AddBlogRequest addBlogRequest , HttpServletRequest request) {
        if(addBlogRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "博客参数不能为空");
        }

        User loginUser = userService.getLoginUser(request);
        if(loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN, "用户未登录");
        }

        Boolean result = blogService.addBlog(addBlogRequest, loginUser);

        return ResultUtils.success(result);
    }

    /**
     * 获取博客列表
     * @param blogListRequest
     * @param request
     * @return
     */
    @ApiOperation(value="获取博客列表", notes="获取博客列表 listBlogByPage")
    @PostMapping("/list/page")
    public BaseResponse<Page<BlogListVO>> listBlogByPage(@RequestBody BlogListRequest blogListRequest, HttpServletRequest request
            )
    {
        if(blogListRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "博客列表参数不能为空");
        }

        User loginUser = userService.getLoginUser(request);
        if(loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN, "用户未登录");
        }

        Page<BlogListVO> page = blogService.listBlogByPage(blogListRequest, loginUser);

        return ResultUtils.success(page);


    }

    /**
     * 获取博客详情
     * @param selectBlogRequest
     * @param request
     * @return
     */
    @ApiOperation(value="获取博客详情", notes="获取博客详情 getBlogById")
    @PostMapping("detail")
    public BaseResponse<SelectBlogVO> getBlogById(@RequestBody SelectBlogRequest selectBlogRequest, HttpServletRequest request) {
        if(selectBlogRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "博客参数不能为空");
        }
        User loginUser = userService.getLoginUser(request);
        if(loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN, "用户未登录");
        }
        SelectBlogVO blog = blogService.getBlogById(selectBlogRequest, loginUser);
        return ResultUtils.success(blog);


    }


}
